﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;

namespace MinecraftGPS.Data
{
    class Bookmark : INotifyPropertyChanged
    {
        private readonly Coordinates position;

        public Bookmark(string name, Coordinates position)
        {
            this.Name = name;
            this.position = position;
        }

        private string name;
        public string Name
        {
            get { return name; }
            set
            {
                if (name == value)
                    return;
                name = value;
                OnPropertyChanged("Name");
            }
        }

        public Coordinates Position { get { return position; } }

        public static void Save(World world, IEnumerable<Bookmark> bookmarks)
        {
            var path = Path.Combine(world.WorldPath, "bookmarks.gps");

            using (StreamWriter writer = new StreamWriter(path))
            {
                foreach (var item in bookmarks)
                {
                    writer.WriteLine(String.Format(
                                         "{0},{1},{2},{3}",
                                         (int)Math.Floor(item.Position.X),
                                         (int)Math.Floor(item.Position.Y),
                                         (int)Math.Floor(item.Position.Z),
                                         item.Name));
                }
            }
        }

        public static IList<Bookmark> Load(World world)
        {
            var path = Path.Combine(world.WorldPath, "bookmarks.gps");

            List<Bookmark> bookmarks = new List<Bookmark>();

            if (File.Exists(path))
            {
                using (StreamReader reader = new StreamReader(path))
                {
                    while (!reader.EndOfStream)
                    {
                        var bookmarkLine = reader.ReadLine();
                        var bits = bookmarkLine.Split(',');
                        var x = Convert.ToInt32(bits[0]);
                        var y = Convert.ToInt32(bits[1]);
                        var z = Convert.ToInt32(bits[2]);
                        var name = String.Join(",", bits, 3, bits.Length - 3);
                        bookmarks.Add(new Bookmark(name, new Coordinates(x, y, z)));
                    }
                }
            }

            return bookmarks;
        }

        private void OnPropertyChanged(string propertyName)
        {
            var handler = this.PropertyChangedEvent;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #region INotifyPropertyChanged Members

        private object eventLock = new Object();

        private event PropertyChangedEventHandler PropertyChangedEvent;
        event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
        {
            add { lock (eventLock) PropertyChangedEvent += value; }
            remove { lock (eventLock) PropertyChangedEvent -= value; }
        }

        #endregion
    }
}
